package com.example.hao.sharedsdk_demo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.mob.pushsdk.MobPush;
import com.mob.pushsdk.MobPushCustomMessage;
import com.mob.pushsdk.MobPushNotifyMessage;
import com.mob.pushsdk.MobPushReceiver;

import java.util.Arrays;
import java.util.HashMap;

import java.util.Map;
import java.util.Set;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import cn.sharesdk.tencent.qq.QQ;


public class MainActivity extends AppCompatActivity {

    public final String TAG = getClass().getSimpleName();
    private MobPushReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobPush.setAlias("abc");//设置别名
        MobPush.addTags(new String[]{"xyz"});//设置标签

        //接收自定义消息
        //接收通知消息
        //接收通知消息被点击事件
        //接收tags的增改删查操作
        //接收alias的增改删查操作
        receiver = new MobPushReceiver() {
            @Override
            public void onCustomMessageReceive(Context context, MobPushCustomMessage message) {
                //接收自定义消息
                Log.d(TAG, "onCustomMessageReceive: "+message.getContent());
            }

            @Override
            public void onNotifyMessageReceive(Context context, MobPushNotifyMessage message) {
                //接收通知消息
                Log.d(TAG, "onNotifyMessageReceive: "+message.getContent());
            }

            @Override
            public void onNotifyMessageOpenedReceive(Context context, MobPushNotifyMessage message) {
                //接收通知消息被点击事件
                Log.d(TAG, "onNotifyMessageOpenedReceive: "+message.getContent());
            }

            @Override
            public void onTagsCallback(Context context, String[] tags, int operation, int errorCode) {
                //接收tags的增改删查操作
                Log.d(TAG, "onTagsCallback: "+ Arrays.toString(tags));
            }

            @Override
            public void onAliasCallback(Context context, String alias, int operation, int errorCode) {
                //接收alias的增改删查操作
                Log.d(TAG, "onAliasCallback: ");
            }
        };
        MobPush.addPushReceiver(receiver);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle bundle = intent.getExtras();
        if (bundle!=null) {
            Set<String> strings = bundle.keySet();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MobPush.removePushReceiver(receiver);
    }

    public void testQQLogin(View view) {
        test();
    }

    /**
     * 调起QQ
     */
    public void test() {
        Platform plat = ShareSDK.getPlatform(QQ.NAME);
        plat.removeAccount(true); //移除授权状态和本地缓存，下次授权会重新授权
        plat.SSOSetting(false); //SSO授权，传false默认是客户端授权，没有客户端授权或者不支持客户端授权会跳web授权
        plat.setPlatformActionListener(new PlatformActionListener() {
            //子线程
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.d(TAG, "onComplete: " + Thread.currentThread().getName());
                //遍历Map
                for (Object o : hashMap.entrySet()) {
                    Map.Entry entry = (Map.Entry) o;
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    Log.i(TAG, "onComplete: " + key + "： " + value);
                }

                //用户资源都保存到res
                //通过打印res数据看看有哪些数据是你想要的
                if (i == Platform.ACTION_USER_INFOR) {
                    PlatformDb platDB = platform.getDb();//获取数平台数据DB
                    //通过DB获取各种数据
                    String token = platDB.getToken();
                    String userGender = platDB.getUserGender();
                    String userIcon = platDB.getUserIcon();
                    String userId = platDB.getUserId();
                    String userName = platDB.getUserName();

                    Log.w(TAG, "onComplete: token " + token);
                    Log.w(TAG, "onComplete: userGender " + userGender);
                    Log.w(TAG, "onComplete: userIcon " + userIcon);
                    Log.w(TAG, "onComplete: userId " + userId);
                    Log.w(TAG, "onComplete: userName " + userName);
                }

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.e(TAG, "onError: " + throwable);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.d(TAG, "onCancel: 取消");
                Toast.makeText(MainActivity.this, "取消!", Toast.LENGTH_SHORT).show();
            }
        });//授权回调监听，监听oncomplete，onerror，oncancel三种状态
        if (plat.isClientValid()) {
            //判断是否存在授权凭条的客户端，true是有客户端，false是无
        }
        if (plat.isAuthValid()) {
//判断是否已经存在授权状态，可以根据自己的登录逻辑设置
            Toast.makeText(this, "已经授权过了", Toast.LENGTH_LONG).show();
            return;
        }
//plat.authorize();	//要功能，不要数据
        plat.showUser(null);    //要数据不要功能，主要体现在不会重复出现授权界面
    }

    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle("西游·降魔篇");

        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl("https://www.baidu.com/s?wd=%E8%A5%BF%E6%B8%B8%E9%99%8D%E9%AD%94&rsv_spt=1&rsv_iqid=0xa739abc400035a9d&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=28035039_3_pg&rsv_enter=0&oq=%25E8%25A5%25BF%25E6%25B8%25B8%25E4%25BC%258F%25E5%25A6%2596%25E7%25AF%2587&rsv_t=506bknR%2BxG8SyaH7I51YHGpVWpOO42w4wasdkWtF2v%2BIKEsYCtNGIKFbU5FTLeAmzyGyRg&inputT=5308&rsv_pq=b915757600004446&rsv_sug1=29&rsv_sug7=100&rsv_sug3=25&bs=%E8%A5%BF%E6%B8%B8%E4%BC%8F%E5%A6%96%E7%AF%87");

        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");

        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl("https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=891cdd1f7a0e0cf3b4fa46a96b2f997a/faf2b2119313b07ed3ce200504d7912397dd8c4f.jpg");

        // url在微信、微博，Facebook等平台中使用
        oks.setUrl("http://sharesdk.cn");

        // comment是我对这条分享的评论，仅在人人网使用
//        oks.setComment("我是测试评论文本");

        // 启动分享GUI
        oks.show(this);
    }

    public void shared(View view) {
        showShare();
    }


}
